package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐相关接口
 */
@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐相关接口:{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id){
        log.info("根据id查询套餐：{}", id);
        SetmealVO setmeal = setmealService.getSetmealById(id);
        return Result.success(setmeal);
    }

    /**
     * 根据id修改套餐起售、停售状态
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("根据id修改套餐起售、停售状态")
    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status,Long id){
        log.info("套餐起售、停售：状态={},id={}", status,id);
        setmealService.status(status,id);
        return Result.success("成功修改套餐状态！");
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    //删除指定分类下的套餐id
    @CacheEvict(cacheNames = "setmeal",key="#setmealDTO.categoryId")
    @ApiOperation("新增套餐")
    @PostMapping
    public Result insertSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐:{}", setmealDTO);
        setmealService.insertSetmeal(setmealDTO);
        return Result.success("新增套餐成功！");
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    //删除分类下的全部套餐id
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除套餐:{}", ids);
        setmealService.delete(ids);
        return Result.success("套餐删除成功！");
    }

    //删除指定分类下的套餐id
    @CacheEvict(cacheNames = "setmeal",key="#setmealDTO.categoryId")
    @ApiOperation("修改套餐")
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐：{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success("套餐修改成功！");
    }

}
